<?php
/**
 * 入口文件
 *
 * 统一入口，进行初始化信息
 *
 *
 * @copyright  Copyright (c) 2007-2013 ShopNC Inc. (http://www.shopnc.net)
 * @license    http://www.shopnc.net/
 * @link       http://www.shopnc.net/
 * @since      File available since Release v1.1
 */
define('BASE_PATH',str_replace('\\','/',dirname(__FILE__)));
require_once(dirname(dirname(__FILE__)).'/global.php');
//htmlentities($value, ENT_QUOTES, 'UTF-8', false)
session_save_path(BASE_DATA_PATH.DS.'session');
require_once(BASE_DATA_PATH.DS.'config/config.ini.php');
if(!empty($config) && is_array($config)){
	$site_url = $config['shop_site_url'];
	$version = $config['version'];
	$setup_date = $config['setup_date'];
	$gip = $config['gip'];
	$dbtype = $config['dbdriver'];
	$dbcharset = $config['db'][1]['dbcharset'];
	$dbserver = $config['db'][1]['dbhost'];
	$dbserver_port = $config['db'][1]['dbport'];
	$dbname = $config['db'][1]['dbname'];
	$db_pre = $config['tablepre'];
	$dbuser = $config['db'][1]['dbuser'];
	$dbpasswd = $config['db'][1]['dbpwd'];
	$lang_type = $config['lang_type'];
	$cookie_pre = $config['cookie_pre'];
}
$act=isset($_GET['act'])?$_GET['act']:'';
$op=isset($_GET['op'])?$_GET['op']:'';
$device=isset($_GET['device'])?$_GET['device']:'';;
if(isset($_SERVER['PATH_INFO'])){
	parse_str(ltrim($_SERVER['PATH_INFO'],'/'),$_temp);
	extract($_temp);
}
header("Content-Type: text/html;charset=UTF-8");
define('SHOP_SITE_URL',$site_url);
switch($act){
	case 'adv':
		define('ATTACH_ADV','shop/adv');
		$advshow_classfile = BASE_PATH.DS.'control/adv.php';
		if(is_file($advshow_classfile)){
			include_once ($advshow_classfile);
			$advshow = new advControl();
			$advshow->advshowOp();
		}else{
			echo "Adv System Inner Error!";
		}
		break;
	case 'toqq':
		session_start();
		if ($op == 'g'){
			include 'api/qq/oauth/qq_callback.php';
		}else{
			if(isset($_SERVER['HTTP_REFERER'])&&!$_SESSION['ref_url']&&!strpos('login',$_SERVER['HTTP_REFERER'])&&preg_match('/^'.preg_quote(SHOP_SITE_URL,'/').'/',$_SERVER['HTTP_REFERER'])){
				$_SESSION['ref_url']=$_SERVER['HTTP_REFERER'];
			}
			$_SESSION['login_device']=$device;
			include 'api/qq/oauth/qq_login.php';
		}
		break;
	case 'tosina':
		session_start();
		if ($op == 'g'){
			include 'api/sina/callback.php';
		}else{
			if(isset($_SERVER['HTTP_REFERER'])&&!$_SESSION['ref_url']&&!strpos('login',$_SERVER['HTTP_REFERER'])&&preg_match('/^'.preg_quote(SHOP_SITE_URL,'/').'/',$_SERVER['HTTP_REFERER'])){
				$_SESSION['ref_url']=$_SERVER['HTTP_REFERER'];
			}
			$_SESSION['login_device']=$device;
			include 'api/sina/index.php';
		}
		break;
	case 'get_session':
		session_start();
		$key = $_GET['key'];
		$val = '';
		if (!empty($_SESSION[$key])) $val = $_SESSION[$key];
		echo $val;
		break;
	case 'sharebind':
		if($_GET['type'] == 'qqzone'){
			include BASE_DATA_PATH.DS.'api/snsapi/qqzone/oauth/qq_login.php';
		}elseif ($_GET['type'] == 'sinaweibo'){
			include BASE_DATA_PATH.DS.'api/snsapi/sinaweibo/index.php';
		}elseif ($_GET['type'] == 'qqweibo'){
			include BASE_DATA_PATH.DS.'api/snsapi/qqweibo/index.php';
		}
		break;
	case 'toweixin':
		session_start();
		if(isset($_SERVER['HTTP_REFERER'])&&!$_SESSION['ref_url']&&!strpos('login',$_SERVER['HTTP_REFERER'])&&preg_match('/^'.preg_quote(SHOP_SITE_URL,'/').'/',$_SERVER['HTTP_REFERER'])){
			$_SESSION['ref_url']=$_SERVER['HTTP_REFERER'];
		}
		echo $_SESSION['weixin_state']=substr(md5(microtime(true)),8,16);
		break;
	case 'toweixin-g':
		session_start();
		include 'api/weixin/callback.php';
		break;
	case 'toalipay':
		session_start();
		if ($op == 'g'){
//			header('location: http://sj.com/api.php/act=toalipay&op=g?'.http_build_query($_GET));
			include 'api/alipay_auth/callback.php';
		}else{
			if(isset($_SERVER['HTTP_REFERER'])&&!$_SESSION['ref_url']&&!strpos('login',$_SERVER['HTTP_REFERER'])&&preg_match('/^'.preg_quote(SHOP_SITE_URL,'/').'/',$_SERVER['HTTP_REFERER'])){
				$_SESSION['ref_url']=$_SERVER['HTTP_REFERER'];
			}
			$_SESSION['login_device']=$device;
			include 'api/alipay_auth/alipay_auth.php';
		}
		break;
	case 'sendSMS':
		include "api/taobao_sms/sendSMS.php";
		break;

}
